package cn.yu.takeoutbackend.employeeManagement.controller;

import cn.yu.takeoutbackend.common.PageSize;
import cn.yu.takeoutbackend.common.Result;
import cn.yu.takeoutbackend.employeeManagement.entity.Employee;
import cn.yu.takeoutbackend.employeeManagement.entity.dto.EmployeeDto;
import cn.yu.takeoutbackend.employeeManagement.service.EmployeeManagementService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController()
@RequestMapping("/employeeManagement")
@Slf4j
public class EmployeeManagementController  {

    @Autowired
    @Qualifier("employeeManagementServiceImpl")
    private EmployeeManagementService managementService;
    @PostMapping("/add")
    public Result addEmployee(@RequestBody Employee employee){
        log.info("----------------  addEmployee start ----------------");
        log.info("Adding employee: {}", employee);
        try {
            managementService.add(employee);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("添加失败");
        }finally {
            log.info("----------------  addEmployee end ----------------");
        }
    }

    public Result deleteEmployee(@RequestBody Employee employee){
        log.info("----------------  deleteEmployee start ----------------");
        log.info("Deleting employee: {}", employee);
        try {
            managementService.delete(employee);
            return Result.success("删除成功");
        }catch (Exception e){
            return Result.error("删除失败");
        }finally {
            log.info("----------------  deleteEmployee end ----------------");
        }
    }

    @PostMapping("/update")
    public Result updateEmployee(@RequestBody Employee employee){
        log.info("----------------  updateEmployee start ----------------");
        log.info("updateEmployee employee: {}", employee);
        try {
            managementService.updateById(employee);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("添加失败");
        }finally {
            log.info("----------------  updateEmployee end ----------------");
        }
    }

    public Result getEmployeeById(@RequestBody Employee employee){
        return null;
    }

    public Result getEmployeeByName(@RequestBody Employee employee){
        return null;
    }
    @PostMapping("/getEmployeeList")
    public Result getEmployeeListWithPageAndCondition(@RequestBody EmployeeDto employeeDto){
        log.info("----------------  getEmployeeListWithPage start ----------------");
        log.info("getEmployeeListWithPage employeeDto: {}", employeeDto);
        try {
            final Page<Employee> listWithPage = managementService.getEmployeeListWithPage(employeeDto);

            final HashMap<String, Object> map = new HashMap<>();
            final PageSize pageSize = new PageSize().setCurrentPage((int) listWithPage.getCurrent())
                    .setPageNum((int) listWithPage.getSize())
                    .setTotal((int) listWithPage.getTotal());
            map.put("pageObject",pageSize);
            map.put("employees",listWithPage.getRecords());
            return Result.success(map);
        }catch (Exception e){
            return Result.error("查询失败");
        }finally {
            log.info("----------------  getEmployeeListWithPage end ----------------");
        }
    }



}
